package com.example.hotelmanagement.controller;

import com.example.hotelmanagement.entity.Hotel;
import com.example.hotelmanagement.service.HotelService;
import com.example.hotelmanagement.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/hotels")
public class HotelController {

    @Autowired
    private HotelService hotelService;

    /**
     * 获取所有酒店信息
     *
     * @return 返回所有酒店信息
     */
    @GetMapping("/")
    public ResponseResult getAllHotels() {
        List<Hotel> hotels = hotelService.getAllHotels();
        return ResponseResult.success("获取酒店列表成功", hotels);
    }

    /**
     * 根据酒店ID获取酒店信息
     *
     * @param hotelId 酒店ID
     * @return 返回指定酒店信息
     */
    @GetMapping("/{hotelId}")
    public ResponseResult getHotelById(@PathVariable Long hotelId) {
        Hotel hotel = hotelService.getHotelById(hotelId);
        if (hotel != null) {
            return ResponseResult.success("获取酒店信息成功", hotel);
        } else {
            return ResponseResult.failure("未找到该酒店");
        }
    }

    /**
     * 新增酒店
     *
     * @param hotel 新酒店信息
     * @return 返回操作结果
     */
    @PostMapping("/")
    public ResponseResult addHotel(@RequestBody Hotel hotel) {
        boolean isAdded = hotelService.addHotel(hotel);
        if (isAdded) {
            return ResponseResult.success("酒店添加成功");
        } else {
            return ResponseResult.failure("酒店添加失败");
        }
    }

    /**
     * 更新酒店信息
     *
     * @param hotelId 酒店ID
     * @param hotel   更新后的酒店信息
     * @return 返回操作结果
     */
    @PutMapping("/{hotelId}")
    public ResponseResult updateHotel(@PathVariable Long hotelId, @RequestBody Hotel hotel) {
        hotel.setId(hotelId);  // 设置酒店ID，以便更新
        boolean isUpdated = hotelService.updateHotel(hotel);
        if (isUpdated) {
            return ResponseResult.success("酒店信息更新成功");
        } else {
            return ResponseResult.failure("酒店信息更新失败");
        }
    }

    /**
     * 删除酒店
     *
     * @param hotelId 酒店ID
     * @return 返回操作结果
     */
    @DeleteMapping("/{hotelId}")
    public ResponseResult deleteHotel(@PathVariable Long hotelId) {
        boolean isDeleted = hotelService.deleteHotel(hotelId);
        if (isDeleted) {
            return ResponseResult.success("酒店删除成功");
        } else {
            return ResponseResult.failure("酒店删除失败");
        }
    }
}
